package ro.dandanciu.webgallery.ui.general;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.UserServices;

import com.opensymphony.xwork2.Preparable;

public class LoginLogoutAction extends WebGalleryAction implements Preparable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8971301761695558853L;

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(LoginLogoutAction.class);

	private static final String LOGIN_PAGE_MODE = "L";
	
	private static final String LOGIN_MODE = "I";
	
	private static final String LOGOUT_MODE = "O";
	
	private static final String LOGIN = "login";
	
	private static final String HOME = "home";
	
	private String mode = null;

	private String userId;

	private String password;
	
	private UserServices userService;
	
	public LoginLogoutAction(UserServices userService) {
		this.userService = userService;
	}
	
	
	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start");
			logger.debug("mode: " + mode);
		}

		String forward = null;
		if(LOGIN_PAGE_MODE.equals(mode)) {
			forward = LOGIN; 
	 	} else if(LOGOUT_MODE.equals(mode)) {
	 		executeLogout();
	 		forward = LOGIN; 
	 	} else  if (LOGIN_MODE.equals(mode)) {
	 		forward = executeLogin();
	 	} else {
	 		forward = setMessage("failure");
	 	}

		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("execute() - end");
		}
		return forward;
	}
	
	private String executeLogin() {
		if (logger.isDebugEnabled()) {
			logger.debug("executeLogin() - start");
		}

		String forward = null;
		User user = userService.getUserByUserIdAndPassword(userId, password);
		if (user != null) {
			setSessionUser(user);
			forward = HOME;
		} else {
			forward = setMessage("login.unkown.user");
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("forward: " + forward);
			logger.debug("executeLogin() - end");
		}
		return forward;
	}
	
	private void executeLogout() {
		if (logger.isDebugEnabled()) {
			logger.debug("executeLogout() - start");
		}

		removeSessionUser();		

		if (logger.isDebugEnabled()) {
			logger.debug("executeLogout() - end");
		}
	}


	
	public void prepare() throws Exception {
		// TODO[prepare] add code

	}


	public String getMode() {
		return mode;
	}


	public void setMode(String mode) {
		this.mode = mode;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserServices getUserService() {
		return userService;
	}

	public void setUserService(UserServices userService) {
		this.userService = userService;
	}

}
